package sk.stuba.fiit.foo07.genex.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;

import sk.stuba.fiit.foo07.genex.beans.Test;
import sk.stuba.fiit.foo07.genex.common.ConnectionHelper;
import sk.stuba.fiit.foo07.genex.common.ResourceHelper;
import sk.stuba.fiit.foo07.genex.common.SettingsHelper;
import sk.stuba.fiit.foo07.genex.export.ExportLatex;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ExportTestDialog extends javax.swing.JDialog {
    /**
     * 
     */
    private static final long serialVersionUID = -8342991578393987116L;

    private Test toExport;

    private ResourceHelper resHelp;
    private JScrollPane jSrcHint;
    private JTextPane jTxtHint;
    private JSeparator jSeparator2;
    private JLabel jLabel4;
    private JTextField jTxtSkupina;
    private JButton jBtnOK;
    private JButton jBtnCancel;
    private JTextField jTxtDate;
    private JLabel jLabel5;
    private JComboBox jComboBox1;
    private JCheckBox jChbPermutate;
    private JButton jBtnFile;
    private JLabel jLabel2;
    private JTextArea jTxtInstructions;
    private JScrollPane jSrcInstructions;
    private JLabel jLabel3;
    private JTextField jTxtName;
    private JLabel jLabel1;
    private JSeparator jSeparator1;
    private String ExportPath;
    public boolean cancel = false;

    public ExportTestDialog(JFrame frame, Test toExport) {
        super(frame);
        resHelp = new ResourceHelper();
        this.toExport = toExport;
        initGUI();
    }

    private void initGUI() {
        try {
            {
                this.setTitle("Exportova\u0165 test");
                this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                this.setLocationByPlatform(true);
                this.setResizable(false);
                this.ExportPath = null;
                getContentPane().setLayout(null);
                {
                    jSrcHint = new JScrollPane();
                    getContentPane().add(jSrcHint);
                    jSrcHint.setBounds(12, 12, 512, 75);
                    {
                        jTxtHint = new JTextPane();
                        jSrcHint.setViewportView(jTxtHint);
                        jTxtHint.setEditable(false);
                        jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
                        jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
                    }
                }
                {
                    jSeparator1 = new JSeparator();
                    getContentPane().add(jSeparator1);
                    jSeparator1.setBounds(12, 93, 512, 10);
                }
                {
                    jSeparator2 = new JSeparator();
                    getContentPane().add(jSeparator2);
                    jSeparator2.setBounds(12, 314, 512, 10);
                }
                {
                    jLabel1 = new JLabel();
                    getContentPane().add(jLabel1);
                    jLabel1.setText("Meno testu");
                    jLabel1.setBounds(12, 109, 106, 14);
                    jLabel1.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtName = new JTextField();
                    getContentPane().add(jTxtName);
                    jTxtName.setBounds(124, 106, 115, 21);
                    jTxtName.setBackground(new java.awt.Color(255, 255, 240));
                    jTxtName.setForeground(new java.awt.Color(0, 0, 255));
                }
                {
                    jBtnFile = new JButton();
                    getContentPane().add(jBtnFile);
                    jBtnFile.setText("...");
                    jBtnFile.setBounds(251, 106, 31, 21);
                    jBtnFile.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnFileActionPerformed(evt);
                        }
                    });
                }
                {
                    jLabel2 = new JLabel();
                    getContentPane().add(jLabel2);
                    jLabel2.setText("\u0160ablóna");
                    jLabel2.setBounds(303, 109, 106, 14);
                    jLabel2.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jLabel3 = new JLabel();
                    getContentPane().add(jLabel3);
                    jLabel3.setText("In\u0161trukcie k testu");
                    jLabel3.setBounds(12, 136, 112, 14);
                    jLabel3.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jSrcInstructions = new JScrollPane();
                    getContentPane().add(jSrcInstructions);
                    jSrcInstructions.setBounds(12, 156, 512, 114);
                    {
                        jTxtInstructions = new JTextArea();
                        jSrcInstructions.setViewportView(jTxtInstructions);
                        jTxtInstructions.setBackground(new java.awt.Color(255,
                                255, 240));
                        jTxtInstructions.setForeground(new java.awt.Color(0, 0,
                                255));
                    }
                }
                {
                    jChbPermutate = new JCheckBox();
                    getContentPane().add(jChbPermutate);
                    jChbPermutate
                            .setText("Zamie\u0161a\u0165 poradie odpovedí");
                    jChbPermutate.setBounds(12, 282, 181, 18);
                    jChbPermutate.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jLabel4 = new JLabel();
                    getContentPane().add(jLabel4);
                    jLabel4.setText("Skupina");
                    jLabel4.setBounds(434, 284, 53, 14);
                    jLabel4.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtSkupina = new JTextField();
                    getContentPane().add(jTxtSkupina);
                    jTxtSkupina.setBounds(493, 281, 31, 21);
                    jTxtSkupina
                            .setBackground(new java.awt.Color(255, 255, 240));
                    jTxtSkupina.setForeground(new java.awt.Color(0, 0, 255));
                }
                {
                    jLabel5 = new JLabel();
                    getContentPane().add(jLabel5);
                    jLabel5.setText("Dátum");
                    jLabel5.setBounds(239, 284, 52, 14);
                    jLabel5.setForeground(new java.awt.Color(100, 0, 0));
                }
                {
                    jTxtDate = new JTextField();
                    getContentPane().add(jTxtDate);
                    jTxtDate.setBounds(303, 281, 82, 21);
                    jTxtDate.setBackground(new java.awt.Color(255, 255, 240));
                    jTxtDate.setForeground(new java.awt.Color(0, 0, 255));
                }
                {
                    jBtnCancel = new JButton();
                    getContentPane().add(jBtnCancel);
                    jBtnCancel.setText("Cancel");
                    jBtnCancel.setBounds(434, 330, 90, 21);
                    jBtnCancel.setMnemonic(java.awt.event.KeyEvent.VK_C);
                    jBtnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnCancelActionPerformed(evt);
                        }
                    });
                }
                {
                    jBtnOK = new JButton();
                    getContentPane().add(jBtnOK);
                    jBtnOK.setText("OK");
                    jBtnOK.setBounds(338, 330, 90, 21);
                    jBtnOK.setMnemonic(java.awt.event.KeyEvent.VK_O);
                    jBtnOK.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            jBtnOKActionPerformed(evt);
                        }
                    });
                }
                {
                    FilenameFilter filter = new FilenameFilter() {
                        public boolean accept(File dir, String name) {
                            return !name.startsWith(".")
                                    && name.endsWith(".vm");
                        }
                    };

                    jComboBox1 = new JComboBox();
                    File templateDir = new File("templates");
                    ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
                            templateDir.list(filter));

                    getContentPane().add(jComboBox1);
                    jComboBox1.setModel(jComboBox1Model);
                    jComboBox1.setBounds(392, 105, 125, 22);
                }
            }
            this.setSize(544, 398);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jBtnFileActionPerformed(ActionEvent evt) {
        JFileChooser jfc;
        try {
            jfc = new JFileChooser(SettingsHelper
                    .getSetting("export_test_path"));
        } catch (Exception e) {
            jfc = new JFileChooser(".");
        }

        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //jfc.setFileFilter(new FileNameExtensionFilter("LaTeX súbory", "tex",
        //        "ltx"));
        int state = jfc.showSaveDialog(this);
        if (state == JFileChooser.CANCEL_OPTION)
            return;

        File f = jfc.getSelectedFile();
        if (f == null)
            return;

        ExportPath = f.getAbsolutePath();
    }

    private void jBtnOKActionPerformed(ActionEvent evt) {
        if (jTxtName.getText().trim().length() == 0) {
            hint("Warning: Názov testu je povinný údaj.");
            return;
        }

        ExportLatex EL = new ExportLatex();
        EL.setGroup(jTxtSkupina.getText());
        EL.setInstructions(jTxtInstructions.getText());
        try {
            //default export directory
            if (ExportPath == null)
                EL.setOutputFilename(SettingsHelper
                        .getSetting("export_test_path")
                        + File.separator + jTxtName.getText());
            //user selected directory
            else
                EL.setOutputFilename(ExportPath + File.separator
                        + jTxtName.getText());
        } catch (IOException e) {
            EL.setOutputFilename(jTxtName.getText());
        }

        EL.setPermutateAnswers(jChbPermutate.isEnabled());
        EL.setTemplate((String) jComboBox1.getSelectedItem());
        EL.setDate(jTxtDate.getText());
        try {
            System.out.print(toExport.getTestID());
            EL.exportTest(toExport.getTestID(), ConnectionHelper
                    .getConnection());
        } catch (IOException e) {
            e.printStackTrace();
        }

        setVisible(false);
    }

    private void hint(String text) {
        if (jTxtHint.getBackground() == GenexGUI.COMB1_BACKGROUND_COLOR) {
            jTxtHint.setBackground(GenexGUI.COMB2_BACKGROUND_COLOR);
            jTxtHint.setForeground(GenexGUI.COMB2_FOREGROUND_COLOR);
        } else {
            jTxtHint.setBackground(GenexGUI.COMB1_BACKGROUND_COLOR);
            jTxtHint.setForeground(GenexGUI.COMB1_FOREGROUND_COLOR);
        }
        if (text.startsWith("Error:")) {
            jTxtHint.setText(text.substring(6));
            jTxtHint.setCaretPosition(0);
            jTxtHint.insertIcon(new ImageIcon(resHelp
                    .getResourceBytes("/icons/error.png")));
        } else if (text.startsWith("Warning:")) {
            jTxtHint.setText(text.substring(8));
            jTxtHint.setCaretPosition(0);
            jTxtHint.insertIcon(new ImageIcon(resHelp
                    .getResourceBytes("/icons/warning.png")));
        } else if (text.startsWith("Info:")) {
            jTxtHint.setText(text.substring(5));
            jTxtHint.setCaretPosition(0);
            jTxtHint.insertIcon(new ImageIcon(resHelp
                    .getResourceBytes("/icons/info.png")));
        } else {
            jTxtHint.setText(text);
        }
    }

    private void jBtnCancelActionPerformed(ActionEvent evt) {
        cancel = true;
        setVisible(false);
    }

    public void showDialog() {
        this.setModalityType(ModalityType.DOCUMENT_MODAL);
        setVisible(true);
    }

}
